package cn.edu.sdjzu.xg.eagle.xg23_kcsj_01.util;


import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FileUtil {
    public static String uploadFile(MultipartFile file, String filePath) throws Exception {
        File targetFile = new File(filePath);
        //
        // if(fileName.equals("")){
//            return null;
//        }
        String originalFilename = file.getOriginalFilename();
        long flag = System.currentTimeMillis();  // 唯一标识
        String fileName = flag + "_" + originalFilename;    //  1561231313132_aaa.png

        //如果路径不存在，创建文件夹
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        //写文件
        FileOutputStream out = new FileOutputStream(filePath + fileName);
        out.write(file.getBytes());
        out.flush(); //清空缓冲区
        out.close();
        return fileName;
    }

    public static boolean deleteFile(String fileName) {
        File file = new File(fileName);
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            if (file.delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }


    public static List<String> getFileNamesFromDir(String path){
        List<String> fns = new ArrayList<String>();
        String[] list=new File(path).list();
        for(int i=0;i<list.length;i++){
            fns.add(list[i]);
        }
        return fns;
    }

    public static void downloadFile(String filename, HttpServletResponse response) throws IOException {
        File file = new File(filename);
        if(file.exists()){ //判断文件是否存在
            // 创建输入对象
            FileInputStream fis = new FileInputStream(file);
            // 设置相关格式
            response.setContentType("application/force-download;charset=UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Content-Disposition", "attachment;fileName=" + filename);
            // 创建输出对象
            OutputStream os = response.getOutputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while(len!= -1) {
                len = fis.read(buf);
                os.write(buf, 0, len);
            }
            System.out.println("----------file download---" + filename);
            fis.close();
        }
    }

    public static Map<String, String> getFilePath(String filePath) {
        //获取指定目录下的所有文件的文件名及其路径
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("文件不存在");
            return null;
        }
        if (!file.isDirectory()) {
            System.out.println("不是目录");
            return null;
        }
        File[] files = file.listFiles();
        if (files == null || files.length == 0) {
            System.out.println("目录下没有文件");
            return null;
        }
        Map<String, String> fileMap = new java.util.HashMap<>();
        for (File f : files) {
            if (f.isFile()) {
                String fileName = f.getName();
                //去除后缀名
                String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf("."));
                //获取文件路径 文件路径是filePath+fileName
                String filePath1 = filePath + fileName;
                fileMap.put(fileNameWithoutExt, filePath1);
            }
        }
        return fileMap;
    }
}
